#include <stdio.h>
#include <stdlib.h>

#include "soci.h"
#include "soci-sqlite3.h"
#include <sqlite3.h>

#include <wx/wx.h>
#include "mediahouseconfig.h"
#include "addon/mhaddon.h"
#include "addon/mhmediaaddon.h"
#include "addon/mhscanneraddon.h"
#include "mhcategory.h"

int main(int argc, char ** argv) {
	{
		hiberlite::Database db("mediahouse.db");
		db.registerBeanClass<mediahouse::CMHMedia> ();
		db.registerBeanClass<mediahouse::CMHCategory> ();
		db.dropModel();
		db.createModel();

		hiberlite::bean_ptr<mediahouse::CMHMedia> b = db.createBean<
				mediahouse::CMHMedia> ();

		b->AddComment(wxT("comment 1"));
		b->AddComment(wxT("comment 2"));
		b->AddComment(wxT("comment 3"));

		b->AddTag(wxT("Tag 1"));
		b->AddTag(wxT("Tag 2"));
		b->AddTag(wxT("Tag 3"));

		b->SetProperty(wxT("name 1"), wxT("value 1"));
		b->SetProperty(wxT("name 2"), wxT("value 2"));
		b->SetProperty(wxT("name 3"), wxT("value 3"));
		b->SetProperty(wxT("category"), wxT("照片"));

		b->SetCreated(wxDateTime::Now());
		b->SetLocation(wxT("测试3423497asdklfjalsdjfiwe982734892734哈哈哈三菱电机发哦isfalsjkdf"));
		b->SetModified(wxDateTime::Now());

		wprintf(L"Id:%ld\n", b.get_id());

		wxString a(wxT("测试"));

		wprintf(L"%ld, %x %x\n", a.size(), (wchar_t) a[0], (wchar_t) a[1]);

		db.close();

		hiberlite::bean_ptr<mediahouse::CMHCategory> c = db.createBean<
				mediahouse::CMHCategory> ();

		c->SetName(wxT("照片"));
		c->SetDisplayName(wxT("照片"));
		c->SetParentId(0);

		hiberlite::bean_ptr<mediahouse::CMHCategory> c1 = db.createBean<
				mediahouse::CMHCategory> ();
		c1->SetName(wxT("香山"));
		c1->SetDisplayName(wxT("香山"));
		c1->SetParentId(c.get_id());

		c1 = db.createBean<mediahouse::CMHCategory> ();
		c1->SetName(wxT("长城"));
		c1->SetParentId(c.get_id());

		c = db.createBean<mediahouse::CMHCategory> ();
		c->SetName(wxT("eBooks"));
		c->SetDisplayName(wxT("电子书"));
		c->SetParentId(0);

		c1 = db.createBean<mediahouse::CMHCategory> ();
		c1->SetName(wxT("小说"));
		c1->SetParentId(c.get_id());

		c1 = db.createBean<mediahouse::CMHCategory> ();
		c1->SetName(wxT("已阅"));
		c1->SetParentId(c.get_id());

		c1 = db.createBean<mediahouse::CMHCategory> ();
		c1->SetName(wxT("漫画"));
		c1->SetParentId(c.get_id());

		c = db.createBean<mediahouse::CMHCategory> ();
		c->SetName(wxT("音乐"));
		c->SetParentId(0);

		c = db.createBean<mediahouse::CMHCategory> ();
		c->SetName(wxT("视频"));
		c->SetParentId(0);
	}

	{
		hiberlite::Database db1("mediahouse.db");
		db1.registerBeanClass<mediahouse::CMHMedia> ();
		hiberlite::bean_ptr<mediahouse::CMHMedia> bb = db1.loadBean<
				mediahouse::CMHMedia> (1);

		wprintf(L"%ls %ls %x %x\n",
				(const wchar_t *) bb->GetCreated().FormatDate().c_str(),
				(const wchar_t *) bb->GetModified().FormatDate().c_str(),
				(wchar_t) bb->GetLocation()[0], (wchar_t) bb->GetLocation()[1]);

		FILE * fp = fopen("test.txt", "wb");
		wxString a(wxT("测试"));
		fprintf(fp, "%d %s=%s\n", a == bb->GetLocation(), a.ToUTF8().data(),
				bb->GetLocation().ToUTF8().data());
		fclose(fp);
	}

	{
		soci::session sql(soci::sqlite3_backend, "mediahouse.db");
		std::vector<std::string> v(10);

		sql << "select distinct item from CMHMedia_CMHTagContainer_m_Tags_items", soci::into(
				v);

		for (int i = 0; i < v.size(); i++) {
			wprintf(L"%s\n", v.at(i).c_str());
		}

		int id;
		soci::indicator ind;
		wxString a(wxT("测试3423497asdklfjalsdjfiwe982734892734哈哈哈三菱电机发哦isfalsjkdf"));
		std::string loc((const char*) a.ToUTF8());

		sql << "select hiberlite_id from CMHMedia where m_Location= :loc", soci::into(
				id, ind), soci::use(loc);

		if (ind == soci::i_ok) {
			wprintf(L"location id =%d\n ", id);
		} else {
			wprintf(L"location is not found\n");
		}
	}

	return 0;
}

